<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import defaultImg from 'assets/images/midea_logo.png';
    import shopBg from "assets/images/shop-header-bg.png";
    import searchIcon from "assets/images/search-ico.png";
    import walletIcon from "assets/images/wallet-icon.png";
    import orderIcon from "assets/images/order-icon.png";
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";

    export default{
        data(){
            return{
                defaultImg: defaultImg,
                shopBg: shopBg,
                searchIcon: searchIcon,
                walletIcon: walletIcon,
                orderIcon: orderIcon,

                sid: null,
                shopInfo: {}, //店铺信息

                catalogList: [ //固定分类有全部
                    ["全部商品","all","all"]
                ],
                goodsList:[],
                catalogType: ["全部商品","all","all"], //默认全部

                /**分页参数**/
                start: 0,
                limit: 9,
                loading: false, //正在加载
                listLoaded: false, //默认的页面没有加载
                listCompleted: false//分页完成
            }
        },
        created() {
            let self = this;
            let config = weex.config;
            self.sid = config.sid;

            if(!self.sid) {
                modal.toast("店铺参数有误");
                return false;
            }

            let _options = {
                sid: self.sid,
                start: self.start,
                limit: self.limit
            };
            api.sendHttp("getSidShopInfoApi",_options).then(res =>{
                self.listLoaded = true;
                self.shopInfo = res.data || {};

                let catArray = res.data.cat || [];
                let groupArray = res.data.group || [];

                catArray.map(function(v,i,arr){
                    return v.push("cat");
                });

                groupArray.map(function(v,i,arr){
                    return v.push("groupid");
                });

                self.catalogList = self.catalogList.concat(catArray.concat(groupArray));

                //默认全部商品
                self.goodsList = res.data.goods || [];
                //总数据量小于limit
                if(self.goodsList.length < self.limit) {
                    self.listCompleted = true;
                }
            },
            res =>{
                modal.toast(res.msg || "获取店铺信息失败");
            });
        },
        methods: {
            sendHttp(succCallback,failCallback){
                let self = this;
                let _options = {
                    sid: self.sid,
                    start: self.start,
                    limit: self.limit
                };

                if(self.catalogType && self.catalogType[1] && self.catalogType[2])  _options[self.catalogType[2]] = self.catalogType[1];

                api.sendHttp("getSidShopListApi",_options).then(res =>{
                    if(typeof succCallback == "function") succCallback(res)
                },
                res =>{
                    modal.toast(res.msg);
                    if(typeof failCallback == "function") failCallback(res)
                });
            },
            fetchMore(){
                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }

                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                self.sendHttp(res=>{
                    self.loading = false;
                    let _goodsList = res.data || [];

                    if(_goodsList.length < self.limit) self.listCompleted = true;
                    self.goodsList = self.goodsList.concat(_goodsList);
                },res=>{
                    self.loading = false;
                });
            },
            initData(){
                let self = this;
                self.goodsList.length = 0;
                self.start = 0;
                self.listCompleted = false;
                self.listLoaded = false;
            },
            sortClick(catalog){
                let self = this;
                if(self.stringifyObj(self.catalogType) == self.stringifyObj(catalog)) {
                    return false;
                }

                //重置参数
                self.initData();
                self.catalogType = catalog;
                self.sendHttp(res=>{
                    self.listLoaded = true;
                    self.goodsList = res.data || [];

                    //总数据量小于limit
                    if(self.goodsList.length < self.limit) {
                        self.listCompleted = true;
                    }
                },res=>{
                     modal.toast(res.msg || "获取店铺商品列表失败");
                });
            },
            stringifyObj(obj){
                obj = obj || {};
                return JSON.stringify(obj);
            },
            goToSearch(){ //跳转原生搜索页
                let self = this;
                let _options = {
                    classType: constant.Search_Class_Type,
                    title: '搜索',
                    params: {
                        sid: self.sid
                    }
                };
                navigator.push(_options);
            },
             goToCoupon(){ //跳转优惠券
                let _options = {
                    url: route.coupon_index,
                    title: '优惠券'
                };
                navigator.push(_options);
            },
            goToOrder(){ //跳转订单
                let _options = {
                    url: route.order_index,
                    title: '我的订单'
                };
                navigator.push(_options);
            }
        }
    }
</script>
